package com.sq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sq.api.dto.SysUserDto;
import com.sq.common.constants.Constants;
import com.sq.common.enums.ReqTypeEnum;
import com.sq.common.exception.param.ParamNotComplete;
import com.sq.common.utils.ParamUtil;
import com.sq.common.utils.StringUtils;
import com.sq.system.mapper.SysUserMapper;
import com.sq.system.pojo.SysRole;
import com.sq.system.pojo.SysUser;
import com.sq.system.pojo.SysUserRole;
import com.sq.system.service.SysRoleService;
import com.sq.system.service.SysUserRoleService;
import com.sq.system.service.SysUserService;
import com.sq.system.utils.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService
{
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public SysUser userinfo(String loginName) {

        // 1. 检验参数
        if (StringUtils.isBlank(loginName)){
            throw new ParamNotComplete("username is not transmission");
        }
        // 2. 逻辑处理

        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();

        sysUserQueryWrapper.eq("user_name",loginName);

        SysUser sysUser = sysUserMapper.selectOne(sysUserQueryWrapper);

        // 3. 结果返回
        return sysUser;
    }

    @Override
    @Transactional
    public int saveSysUser(SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        String idStr = IdWorker.getIdStr();
        BeanUtils.copyProperties(sysUserDto,sysUser);
        sysUser.setUserId(idStr);
        ParamUtil.checkParam(sysUser, ReqTypeEnum.insert);
        boolean save = save(sysUser);
        List<String> roleIds = sysUserDto.getRoleIds();
        if (Objects.isNull(roleIds) || roleIds.isEmpty()){
            SysRole sysRole = sysRoleService.selectRoleByRoleName(Constants.commonRole);
            String roleId = sysRole.getRoleId();
            roleIds = new ArrayList<>();
            roleIds.add(roleId);
        }
        Integer i = sysUserRoleService.saveUserRole(roleIds, idStr);
        return (save && i > 0) ? 1 : 0;
    }


    @Override
    public List<SysUser> selectWhere(SysUser sysUser) {
        List<SysUser> list = sysUserMapper.selectWhere(sysUser);
        return list;
    }

    @Override
    public List<SysUser> selectVo(SysUser sysUser) {
        List<SysUser> list = sysUserMapper.selectVo(sysUser);
        return list;
    }

    @Override
    public boolean deleteUserById(String userId) {
        int i = sysUserMapper.deleteById(userId);
        return i > 0 ? true : false;
    }

    @Override
    @Transactional
    public boolean updateUser(SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto,sysUser);
        ParamUtil.checkParam(sysUser,ReqTypeEnum.update);
        sysUser.setUpdateBy(UserInfoUtil.getUserName());
        sysUser.setUpdateTime(new Date());
        int updateUserInt = sysUserMapper.updateById(sysUser);

        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",sysUser.getUserId());
        boolean remove = sysUserRoleService.remove(queryWrapper);

        if (remove){
            List<String> roleIds = sysUserDto.getRoleIds();
            Integer saveUserRoleInt = sysUserRoleService.saveUserRole(roleIds, sysUser.getUserId());
            return updateUserInt > 0  && saveUserRoleInt > 0? true : false;
        }

        return false;
    }
}




